<#include "/includes/vars.inc">  
<@pp.dropOutputFile />

<#list entities.entity as entity>
	<#if entity.@singleton="false">
	<#assign entityName = entity.@name>
	<#assign fileName = "${testJavaDir}${packageDir}/service/"+entityName?cap_first+"IntegrationTest.java">
	<@pp.changeOutputFile name="${fileName}" />
		
package ${project.@rootPackage}.service;

import ${project.@rootPackage}.domain.${entityName};
import ${project.@rootPackage}.util.${entityName}DataGenerator;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Integration Test for entity ${entityName}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test.xml")
@Component
public class ${entityName}IntegrationTest {

    @Autowired
    private ${entityName}Service service;
    @Autowired
    private ${entityName}DataGenerator data;

    @Test
    public void testCreate() throws Exception {
        List<${entityName}> before = service.findAll();
        Assert.assertNotNull(before);
        ${entityName} ${entityName?uncap_first} = data.getNewTransientEntity(1);
        Assert.assertNotNull(${entityName?uncap_first});
        service.create(${entityName?uncap_first});
        List<${entityName}> after = service.findAll();
        Assert.assertNotNull(after);
        Assert.assertTrue(before.size() + 1 == after.size());
    }

    @Test
    public void testUpdate() throws Exception {
        ${entityName} ${entityName?uncap_first} = data.getRandomEntity();
        ${entityName} other = data.getNewTransientEntity(2);
        other.setId(${entityName?uncap_first}.getId());
        ${entityName?uncap_first}=null;
        service.update(other);
        ${entityName} result = service.findById(other.getId());
        Assert.assertNotNull(result);
        Assert.assertEquals(other.getId(), result.getId());
    }

<#--
    @Test
    public void testDelete() throws Exception {
        ${entityName} delete = data.getRandomEntity();
        Long id = delete.getId();
        System.out.println("${entityName} delete:" + delete);
        Assert.assertNotNull(delete);
        service.delete(delete);
        ${entityName} result = service.findById(id);
        System.out.println("${entityName} result:" + result);
        Assert.assertNull(result);
    }
-->

    @Test
    public void testFindAll() throws Exception {
        List<${entityName}> result = service.findAll();
        Assert.assertNotNull(result);
        System.out.println(result.toString());
    }

    @Test
    public void testFindById() throws Exception {
        ${entityName} ${entityName?uncap_first} = data.getRandomEntity();
        ${entityName} result = service.findById(${entityName?uncap_first}.getId());
        Assert.assertNotNull(result);
        Assert.assertEquals(${entityName?uncap_first},result);
    }
    
    
    //TODO: testCreate(Entities)
    //TODO: testUpdate(Entities)
    //TODO: testDelete(Entities)
}
	</#if>
</#list>